from task import Task

#http://docs.python.org/library/functions.html#list
#http://defcraft.blogspot.com/2007/04/n-based-python-list.html
class Tasks(list):
    def __init__(self, fileobj=None):
        if fileobj: #+1 for 1 based counting to match todo.txt
            temp = [Task(tsk, id=lineno+1) for lineno,tsk in enumerate(fileobj.readlines())]
            list.__init__(self, temp)
            self.sort(key=lambda x: x._cmpval)

    def insert(self, i, x):
        raise NotImpemented()

    def append(self, y):
        """override append to sort"""
        y.id = len(self)+1  #1 based counting
        super(Tasks, self).append(y)
        self.sort(key=lambda x: x._cmpval)

    #http://defcraft.blogspot.com/2007/04/n-based-python-list.html
    #http://docs.python.org/tutorial/#looping-techniques
    def enumerate(self):
        lpad = len(str(len(self)))
        for task in self:
            yield str(task.id).zfill(lpad), task